<?php

namespace App\Http\Controllers\Admin;

use App\Models\Item;
use App\Models\ItemPush;
use App\Models\ItemPushGroup;

class ItemPushController extends BaseController
{

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function index()
    {

        if ($this->isOnSubmit()) {
            $delete = $this->request->input('delete');
            if ($delete) {
                foreach ($delete as $groupid) {
                    ItemPush::where('groupid', $groupid)->delete();
                    ItemPushGroup::where('groupid', $groupid)->delete();
                }
            }

            $grouplist = $this->request->post('grouplist');
            if ($grouplist) {
                foreach ($grouplist as $groupid=>$group) {
                    if ($group['grouptitle']) {
                        if ($groupid > 0) {
                            ItemPushGroup::where('groupid', $groupid)->update($group);
                        } else {
                            ItemPushGroup::insert($group);
                        }
                    }
                }
            }

            return $this->messager()->render();
        } else {
            $this->assign(['grouplist'=>ItemPushGroup::all()]);
            return $this->view('admin.item.push');
        }
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     * @throws \Exception
     */
    public function itemlist()
    {
        if ($this->isOnSubmit()) {
            $delete = $this->request->post('delete');
            if ($delete) {
                foreach ($delete as $id) {
                    ItemPush::where('id', $id)->delete();
                }
            }

            $items = $this->request->input('items');
            if ($items)
            {
                foreach ($items as $key=>$id)
                {
                    ItemPush::where('id', $id)->update(['displayorder'=>$key]);
                }
            }
            return $this->messager()->render();
        } else {

            $query = ItemPush::with(['item', 'group']);
            $groupid = $this->request->input('groupid');
            if ($groupid) {
                $query = $query->where('groupid', $groupid);
            }

            $items = $query->orderBy('displayorder')->paginate(20);
            $this->assign([
                'groupid'=>$groupid,
                'pagination'=>$items->appends(['groupid'=>$groupid])->render(),
                'items'=>$items->map(function ($item){
                    if ($item->item) {
                        if ($item->item->on_sale){
                            $item->item->grouptitle = $item->grouptitle;
                            return $item;
                        } else {
                            $item->delete();
                        }
                    } else {
                        $item->delete();
                    }
                }),
                'grouplist'=>ItemPushGroup::all()
            ]);

            return $this->view('admin.item.push_item');
        }
    }

    public function add()
    {
        $id = $this->request->input('id');
        $itemid = $this->request->post('itemid');
        $groupid = $this->request->post('groupid');
        $title = $this->request->post('title');

        $item = Item::where('itemid', $itemid)->first(['title', 'thumb', 'image']);
        if ($item) {
            !$title && $title = $item->title;

            if ($id) {
                ItemPush::where('id', $id)->update([
                    'itemid'=>$itemid,
                    'title'=>$title
                ]);
            }else {
                ItemPush::insert([
                    'itemid'=>$itemid,
                    'title'=>$title,
                    'image'=>$item->thumb,
                    'groupid'=>$groupid,
                ]);
            }
        }
        return ajaxReturn();
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function setimage()
    {
        $id = $this->request->input('id');
        $image = $this->request->input('image');
        if ($id && $image) {
            ItemPush::where('id', $id)->update(['image'=>$image]);
        }

        return ajaxReturn();
    }
}
